home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / adscraper.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.7 KB  |  74 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import re
  19.  
  20. # =============================================================================
  21.  
  22. # Purify a feed item data (usually its description) from ads. Returns the data
  23. # untouched if no ad were found.
  24. def purify(data):
  25.     return _process(data, 'purify', data)
  26.  
  27. # Scrape ads from a feed item data (usually its description). Returns an empty
  28. # string if no ad were found.
  29. def scrape(data):
  30.     return _process(data, 'scrape', '')
  31.  
  32. # =============================================================================
  33.  
  34. def _process(data, fkey, default):
  35.     if data is None:
  36.         return ''
  37.     processed = None
  38.     for funcs in FUNCS:
  39.         process = funcs[fkey]
  40.         processed = process(data)
  41.         if processed is not None:
  42.             break
  43.     if processed is None:
  44.         processed = default
  45.     return processed
  46.  
  47. # =============================================================================
  48.  
  49. FEEDBURNER_AD_PATTERN = re.compile("""
  50.     <p>                                                               # <p>
  51.     <a\shref="http://feeds\.feedburner\.com/~a/[^"]*">                # <a href="...">
  52.     <img\ssrc="http://feeds\.feedburner\.com/~a/[^"]*"\sborder="0">   # <img src="..." border="0">
  53.     </img>                                                            # </img>
  54.     </a>                                                              # </a>
  55.     </p>                                                              # </p>
  56.     """, re.VERBOSE)
  57.     
  58. def _tryPurifyingFeedBurner(data):
  59.     if FEEDBURNER_AD_PATTERN.search(data):
  60.         return FEEDBURNER_AD_PATTERN.sub('', data)
  61.     return None
  62.  
  63. def _tryScrapingFeedBurner(data):
  64.     match = FEEDBURNER_AD_PATTERN.search(data)
  65.     if match is not None:
  66.         return match.group(0)
  67.     return None
  68.  
  69. # =============================================================================
  70.  
  71. FUNCS = [
  72.     {'purify': _tryPurifyingFeedBurner, 'scrape': _tryScrapingFeedBurner}
  73. ]
  74.